import java.security.InvalidParameterException;
import java.time.LocalDateTime;
import java.util.Date;

public class Calculate {

    private Date startTime;
    private Date endTime;
    private boolean flag1;
    private boolean flag2;

    public Calculate(Date startTime, Date endTime, boolean flag1, boolean flag2) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.flag1 = flag1;
        this.flag2 = flag2;
    }

    public Calculate() {
    }

    /**
     *
     * @return 返回两个时间差的分钟数
     */
    public int calcPhoneTime(){

        long start = startTime.getTime();
        long end = endTime.getTime();

        long minutes = (end - start)/1000/60;

        if ((end - start) % 60000 != 0) {
            minutes ++;
        }
        if (flag1 && flag2) {
            throw new FlagErrorException("标志位错误");
        }
        else if (flag1) {
            minutes -= 60;
        }
        else if (flag2) {
            minutes += 60;
        }
        System.out.println("分钟数：" + minutes);
        if (minutes <= 0) {
            throw new InvalidParameterException("参数错误");
        }
        else if (minutes >= 1800) {
            throw new ChatTimeOverException("通话时间超时");
        }
        return (int) minutes;
    }

    /**
     * 计算话费
     * @return 返回话费
     */
    public double calcBill(){
        int minutes = calcPhoneTime();
        double charge;
        if (minutes > 20) {
            charge = 1 + (minutes-20)*0.1;
        }
        else {
            charge = minutes * 0.05;
        }
        return charge;
    }

}
